/* 
 *   Classname:  config.TestController
 * 
 *   Date       Developer   Description
 *   2017年8月13日    周成功              新建
 *
 */
package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.titan.core.utils.data.CollectionUtils;
import com.titan.core.utils.data.DateUtils;
import com.titan.core.utils.data.FormatUtils;
import com.titan.core.utils.data.ListUtils;
import com.titan.core.utils.db.JdbcUtils;
import com.titan.core.utils.io.ConsoleUtils;
import com.titan.core.utils.object.VariantSix;
import com.titan.core.utils.spring.SpringUtils;
import com.titan.demo.commons.Where.Player1MoneyWhere;
import com.titan.demo.commons.dao.IPlayer1MoneyDao;
import com.titan.demo.commons.domain.Player1MoneyDomain;

import zzz.GenerateCode.v4.base.BaseGCv4Controller;
import zzz.GenerateCode.v4.domain.HtmlElementProperty;

/**
 *
 *
 * @author 周成功
 *
 */
@RestController
@RequestMapping("/test")
public class TestController extends BaseGCv4Controller {
    @RequestMapping(value="/action")
    public void action(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ConsoleUtils.printParameter(request);
        try {
            long time = System.currentTimeMillis();
            outInit(request, response, "查询测试");
            
            String play = request.getParameter("play")==null?"":request.getParameter("play");
            String playid = request.getParameter("playid")==null?"":request.getParameter("playid");
            
            outForm(request.getContextPath()+"/test/action", ListUtils.toList(
                     HtmlElementProperty.$().setLabel("play").setType("text").setId("play").setName("play").setValue(play)
                   , HtmlElementProperty.$().setLabel("playid").setType("text").setId("playid").setName("playid").setValue(playid)
                   , HtmlElementProperty.$().setType("submit").setValue("查询")
                   ));
            outNewLine();
            
            IPlayer1MoneyDao dao = SpringUtils.getBean(IPlayer1MoneyDao.class);
            Player1MoneyWhere where = Player1MoneyWhere.$("createtime desc")
                    .andPlayidLike(playid).andPlayLike(play);
            
            List<Player1MoneyDomain> items = dao.findByWhere("", where);
            outHtml("<div style='color:blue;'>记录数："+items.size()+"</div>");
            List<VariantSix> data = new ArrayList<VariantSix>();
            data.add(VariantSix.$("日期时间", "项目", "记录id", "原余额", "变动", "现余额"));
            for (int i = 0; i < items.size(); i++) {
                data.add(VariantSix.$(
                      DateUtils.$(items.get(i).getCreatetime()).toString("yyyy-MM-dd HH:mm:ss")
                    , items.get(i).getPlay()
                    , items.get(i).getPlayid()
                    , items.get(i).getMoney_old()
                    , items.get(i).getMoney_var()
                    , items.get(i).getMoney_new()
                    ));
            }
            outTable(data);
            outHtml("<div style='color:blue;'>用时："+(System.currentTimeMillis()-time)+"&nbsp;毫秒</div>");
            outEnd();
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }
    
    @RequestMapping(value="/sysdate")
    public void sysdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ConsoleUtils.printParameter(request);
        try {
            long time = System.currentTimeMillis();
            outInit(request, response, "查询数据库库日期");
            
            Date date = JdbcUtils.queryForDate("select sysdate()");
            outHtml("<div style='color:blue;'>当前数据库时间为："+FormatUtils.dateToString(date, "yyyy-MM-dd HH:mm:ss.SSS")+"</div>");
            outEnd();
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }    
}
